<template>
    <div id="member">
        <el-col :span="24" class="action-bar">
            <el-button type="primary" size="medium" @click="signatureVisible=true">添加</el-button>
        </el-col>
        

        <!-- 列表 -->
        <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="date"
                label="头像"
                width="100">
                <template slot-scope="scope">
                        <img :src="scope.row.photo_url" alt="" style="width:60px;height:60px;object-fit:cover">
                </template>
            </el-table-column>
            <el-table-column
                prop="nickname"
                label="昵称"
                width="200"
                >
            </el-table-column>
            <el-table-column
                prop="date"
                label="性别"
                width="100">
                <template slot-scope="scope">
                        <div>
                            <div v-if="scope.row.sex==1">男</div>
                            <div v-else-if="scope.row.sex==2">女</div>
                            <div v-else>男</div>
                        </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="signature"
                label="生日"
                width="200"
                >
                <template slot-scope="scope" v-if="scope.row.birthday">
                    <div>{{scope.row.birthday.split(' ')[0]}}</div>
                </template>
            </el-table-column>
            <el-table-column
                    prop="manager_name"
                    label="所属管理员"
                    width="200"
            >
            </el-table-column>
            
            <el-table-column label="操作" width='143'>
                <template slot-scope="scope">
                    <el-button type='text' @click="editAccount(scope.row.user_id)">编辑</el-button>
                    <el-button type='text' @click="deleteAccount(scope.row.user_id)">删除</el-button>
                </template>
            </el-table-column>
            
        </el-table>
        <el-col :span="24" class="footer-bar">
            <div class="page-code">
                <el-pagination
                        background
                        :page-size="16"
                        layout="prev, pager, next"
                        :total="totalPage"
                        @current-change="pageChange"
                >
                </el-pagination>
            </div>
        </el-col>

		<el-dialog title="帖子类型" :visible.sync="signatureVisible" @close="initData">
            <el-form>
                <el-form-item label="头像" >
                    <el-upload
                        class="avatar-uploader"
                        action="https://bibiji.rzkeji.com/api/file/upload-img"
                        name="photo"
                        :show-file-list="false"
                        :on-success="handleLogo"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="form.logoImg" :src="form.logoImg" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                </el-form-item>
                <el-form-item label="昵称" >
                    <el-input palceholder="昵称" v-model="form.nickName" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="性别" >
                    <el-select v-model="form.sex" clearable placeholder="性别" style="margin:0 20px;">
                        <el-option
                        v-for="item in sexOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" >
                    <el-date-picker
                        v-model="form.birth"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="个性签名" >
                    <el-input palceholder="个性签名" v-model="form.signature" style="width:300px"></el-input>
                </el-form-item>
                <el-form-item label="选择管理员">
                    <el-select v-model="form.adminId" clearable placeholder="选择管理员" >
                        <el-option
                                v-for="item in adminList"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                
            </el-form>
            <div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submit" >提交</el-button>
			</div>
		</el-dialog>
    </div>
</template>
<script>
export default {
    name:'member',
    data() {
        return {
            name:'',
            signatureVisible:false,//编辑弹窗
            circleTypeId:'',//选中的用户id
            tableData: [{}],
            page:1,
            totalPage:0,
            form:{
                logoImg:'',
                logoId:'',
                nickName:'',
                sex:'',
                signature:'',
                adminId:'',
                id:0
            },
            sexOptions:[{label:'男',value:'1'},{label:'女',value:'2'},{label:'保密',value:'0'}],
            adminList:[],
            limit:30
        }
    },
    methods: {     
         beforeAvatarUpload(file) {
			//上传照片前的操作
			// console.log(file,555)
		},
        handleLogo(res, file) {
			//上传头像成功
			if(res.data){
				this.form.logoImg=res.data.img_url;
                this.form.logoId=res.data.id;
                console.log(this.form.logoId,666)
			}
        },
        //获取管理员列表
        async getAdminList(page){
            let params = {
                limit:this.limit,
                page:page || 1,
            }
            const res = await this.$api.getAdminList(params);
            if(res.data.level=='success'){
                let list = res.data.data;
                this.adminList = list;
            }else{
                this.$message({
                    message: res.data.message,
                    type: 'warning'
                });
            }
        },
        async getOperationNumList(){
            let params={
                page:this.page,
                limit:16,
            }
            let res=await this.$api.getOperationNumList(
                params);
            if(res.data.level=='success'){
                this.tableData=res.data.data.map(item=>{return item.user_info});
                this.totalPage=res.data.page_info.total;
            }
        },
        pageChange(e){
			//翻页
			this.page=e;
			this.getOperationNumList();
        },
        async submit(){
            let params={
                photo_id:this.form.logoId,
                nickname:this.form.nickName,
                birthday:this.form.birth,
                signature:this.form.signature,
                sex:this.form.sex,
                manager_id:this.form.adminId,
                id:this.form.id,
            }
            let res=await this.$api.setOperationNum(params)
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.getOperationNumList();
                this.signatureVisible=false
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
                
            }
        },
        async editAccount(id){
            this.signatureVisible = true
            let res=await this.$api.getOperateAcount({id})
            if(res.data.level=='success'){
                let item  = res.data.data
                this.form.logoImg = item.user_info.photo_url;
                this.form.logoId = item.user_info.photo_id;
                this.form.nickName = item.user_info.nickname;
                this.form.signature = item.user_info.signature;
                this.form.sex = item.user_info.sex;
                this.form.adminId = item.manager_operate != null ? item.manager_operate.manager_id : 0;
                this.form.id = item.id;
                this.form.birth = item.user_info.birthday;
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })

            }
        },
        async deleteAccount(id){
            //删除圈子类型
             let judge=null
            try {
              judge=  await this.$confirm('删除该账号?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
                })
            } catch (error) {
                judge='cancel'
            }
            if(judge=='confirm'){
                let res=await this.$api.delOperationNum({id})
                if(res.data.level=='success'){
                this.$message({
                        type:'success',
                        message:res.data.message
                    })
                    this.getOperationNumList();
                }else{
                    this.$message({
                        type:'error',
                        message:res.data.message
                    })
                
                }
            }
            console.log(judge)
        },
        initData(){
            this.form=this.$options.data().form
        }

    },
    mounted() {
        this.circleId=this.$route.query.id;
        this.getOperationNumList();
        this.getAdminList();
    },
}
</script>
<style lang="scss" scoped>
.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
		border: 1px dashed #d9d9d9;
	}
	.avatar {
		width: 178px;
		height: 178px;
		display: block;
		object-fit: contain;
    }
.screen{
          margin-top: -10px !important;
      }
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
			margin: 10px 0px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
      }
      .task-form-text{
				color: #666666;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #f2f2f2;
			padding: 8px;
			margin: 10px 0px;
			box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
		.executorList{
			display: flex;
			flex-wrap: wrap;
			div{
				margin:10px;
				border:1px solid #eee;
				border-radius: 5px;
				padding:5px 10px;
			}
		}
</style>